Page({
    data: {
        currentVideoId: null, // 当前播放的视频ID
        completedCount: 3, // 已完成关卡数
        totalCount: 10, // 总关卡数
        progress: 30, // 进度条百分比
        videoList: []
    },

    /* 视频设置 */
    // 视频播放事件处理
    handleVideoPlay(e) {
        const index = e.currentTarget.dataset.index;
        const newVideoId = `video${index}`;
        const currentVideoId = this.data.currentVideoId;

        // 如果点击的是当前正在播放的视频
        if (currentVideoId === newVideoId) return;

        // 暂停之前播放的视频
        if (currentVideoId) {
            this[currentVideoId] && this[currentVideoId].pause();
        }

        // 创建并保存新的视频上下文
        if (!this[newVideoId]) {
            this[newVideoId] = wx.createVideoContext(newVideoId, this);
        }

        // 更新当前播放视频ID
        this.setData({
            currentVideoId: newVideoId
        });

    },

    /* 视频自动播放下一个 */
    handleVideoEnd(e) {
        const currentIndex = e.currentTarget.dataset.index;
        const nextIndex = currentIndex + 1;

        if (nextIndex < this.data.videoList.length) {
            const nextVideoId = `video${nextIndex}`;

            // 确保上下文存在
            if (!this[nextVideoId]) {
                this[nextVideoId] = wx.createVideoContext(nextVideoId, this);
            }

            // 先暂停当前视频
            const currentVideoId = `video${currentIndex}`;
            this[currentVideoId] && this[currentVideoId].pause();

            // 更新状态并播放
            this.setData({
                currentVideoId: nextVideoId
            }, () => {
                this[nextVideoId].play();
            });
        }
    },
    // 关闭弹窗
    closePopup() {
        this.setData({
            showPopup: false
        })
    },

    // 阻止事件冒泡
    stopPropagation() {},
    //生命周期
    onLoad() {

        wx.request({
            url: 'http://47.111.115.82:8080/uploadVideo', //仅为示例，并非真实的接口地址
            method: "POST",
            header: {
                // 'content-type': 'application/json' ,// 默认值
                // 'jwt':wx.getStorageSync('token')
            },
            success(res) {
                console.log(res.data.data)
                wx.setStorageSync('list', res.data.data)
            }
        })
        this.setData({
            videoList: wx.getStorageSync('list')
        })
        this.setData({
            showPopup: true
        }, )
    },
})